package de.baumann.browser.activitys.order

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.webkit.SslErrorHandler
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import de.baumann.browser.R
import de.baumann.browser.api.net.vo.PayParam
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.IBaseView
import de.baumann.browser.js.PayJs
import de.baumann.browser.utils.ViewUtil

@Deprecated("功能已删除")
class WechartPayActivity : BaseOdinActivity<IBaseView, BasePresenter<IBaseView>>(), PayJs.PaySuccessCallBack {

    lateinit var wxPayView: WebView
    lateinit var payParam: PayParam

    override fun getTitleText(): String {
        return getString(R.string.WeChat_payment)
    }

    override fun getLayout(): Int {
        return R.layout.activity_wechart_pay
    }

    @SuppressLint("JavascriptInterface")
    override fun initView() {
        wxPayView = findViewById(R.id.wxPayView)
        ViewUtil.configWebView(wxPayView)
        wxPayView.webViewClient = webViewClient
    }

    private var webViewClient: WebViewClient = object : WebViewClient() {

        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            if (url!!.startsWith("weixin://wap/pay?")) {
                if (ViewUtil.isInstalledWX(mContext)) {
                    val intent = Intent()
                    intent.action = Intent.ACTION_VIEW
                    intent.data = Uri.parse(url)
                    startActivity(intent)
                } else {
                    showShortToast(getString(R.string.not_installed_WeChat))
                }
                return true
            } else {
                view!!.addJavascriptInterface(PayJs(payParam, this@WechartPayActivity), "pay")
                val extraHeaders = java.util.HashMap<String, String>()
                extraHeaders["Referer"] = "http://pay.voleedu.cn"
                view.loadUrl(url, extraHeaders)
            }
            return true
        }

        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
            return shouldOverrideUrlLoading(view, request!!.url.toString())
        }

        // 重写此方法可以让webview处理https请求
        override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: android.net.http.SslError) {
            handler.proceed()
        }
    }

    override fun createPresenter(): BasePresenter<IBaseView>? {
        return null
    }

    override fun createView(): IBaseView? {
        return null
    }


    override fun initData() {
        payParam = intent.getSerializableExtra("payParam") as PayParam
        wxPayView.addJavascriptInterface(PayJs(payParam), "pay")
        wxPayView.loadUrl("http://pay.voleedu.cn/page/wxpay.html?" + System.currentTimeMillis())
    }

    override fun onPaySuccess() {
        setResult(Activity.RESULT_OK)
        finish()
    }
}
